package com.liu.producer.handler;

import com.liu.message.impl.ResponseMessage;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.util.concurrent.Promise;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.ConcurrentHashMap;

/**
 * @param
 * @BelongsProject: MessageQueueDEMO
 * @BelongsPackage: com.liu.producer.handler
 * @author: L
 * @Date: 2023/10/15 3:03
 * @return
 */
@Slf4j
@ChannelHandler.Sharable
public class ResponseMessageHandler extends SimpleChannelInboundHandler<ResponseMessage> {
    public static final ConcurrentHashMap<Long, Promise<Object>> PROMISE_MAP = new ConcurrentHashMap<>();

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, ResponseMessage msg) {
        log.info(msg.getMessageID()+"--->enter ResponseMessageHandler");
        Promise<Object> promise = PROMISE_MAP.remove(msg.getMessageID());
        if (msg.getExceptionValue() != null) {
            promise.setFailure(msg.getExceptionValue());
        } else {
            promise.setSuccess(msg.getReturnValue());
        }
    }
}
